package portal.bean;

import javax.faces.context.FacesContext;

import javax.servlet.http.HttpServletRequest;

import oracle.adf.view.rich.render.ClientEvent;

import oracle.webcenter.framework.service.AnalyticsUtil;

public class AnalyticsInstrumentation {
    public AnalyticsInstrumentation() {
        super();
    }

    public void sendPageEvent(ClientEvent event) {
        // Checks whether Analytics events must be sent
        if (!AnalyticsUtil.isSendingEvents())
            return;

        HttpServletRequest request =
            (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
        String requestUser = request.getRemoteUser();

        //SEND PAGE VIEW EVENT
        String pageName = (String)event.getParameters().get("pageName");
        if (pageName == null || pageName.isEmpty() || requestUser == null)
            return;

        AnalyticsUtil.sendPageViewEvent(FacesContext.getCurrentInstance().getViewRoot().getViewId(),
                                        //viewID
                "PortalApp", //spaceDisplayName
                requestUser, //username
                0, pageName, false, request);
    }
}
